﻿namespace QLPhongKham.BaseClass.BenhNhan
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoBNThongTinBN_LienHe
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private long _deidql;
        private string _sdiachi;
        private string _sdienthoai;
        private string _shoten;
        private string _smabn;
        private string _squanhe;
        private string _sSchema;

        public dtoBNThongTinBN_LienHe()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoBNThongTinBN_LienHe(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "BN_THONGTINBN_LIENHE.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1 and ", columnIDQL, " = ", this._deidql }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "BN_THONGTINBN_LIENHE.cs");
                return false;
            }
        }

        public decimal GetIDInsert()
        {
            decimal iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "BN_THONGTINBN_LIENHE.cs");
                iDInsert = 1M;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public decimal GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                decimal d = 0M;
                try
                {
                    d = decimal.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return (d++);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "BN_THONGTINBN_LIENHE.cs");
                return 1M;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "BN_THONGTINBN_LIENHE.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnIDQL, ",", columnMABN, ",", columnQUANHE, ",", columnHOTEN, ",", columnDIACHI, ",", columnDIENTHOAI, 
                    ") values(", this._deidql, ",'", this._smabn, "',:", columnQUANHE, ",:", columnHOTEN, ",:", columnDIACHI, ",'", this._sdienthoai, "')"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnQUANHE, NpgsqlDbType.Text, 200).Value = this._squanhe;
                command.Parameters.Add(columnHOTEN, NpgsqlDbType.Text, 200).Value = this._shoten;
                command.Parameters.Add(columnDIACHI, NpgsqlDbType.Text, 200).Value = this._sdiachi;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "BN_THONGTINBN_LIENHE.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deidql = 0L;
            this._smabn = "";
            this._squanhe = "";
            this._shoten = "";
            this._sdiachi = "";
            this._sdienthoai = "";
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "BN_THONGTINBN_LIENHE.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnIDQL, ",", columnMABN, ",", columnQUANHE, ",", columnHOTEN, ",", columnDIACHI, ",", columnDIENTHOAI, " from ", this._sSchema, ".", tableName, 
                    " where ", columnIDQL, "=", this._deidql
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deidql = long.Parse(dataTable.Rows[0][columnIDQL].ToString());
                    this._smabn = dataTable.Rows[0][columnMABN].ToString();
                    this._squanhe = dataTable.Rows[0][columnQUANHE].ToString();
                    this._shoten = dataTable.Rows[0][columnHOTEN].ToString();
                    this._sdiachi = dataTable.Rows[0][columnDIACHI].ToString();
                    this._sdienthoai = dataTable.Rows[0][columnDIENTHOAI].ToString();
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "BN_THONGTINBN_LIENHE.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect, 0L, "");
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "BN_THONGTINBN_LIENHE.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(long idql)
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect, idql, "");
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "BN_THONGTINBN_LIENHE.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(string mabn)
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect, 0L, mabn);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "BN_THONGTINBN_LIENHE.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(long idql, string mabn)
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect, idql, mabn);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "BN_THONGTINBN_LIENHE.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect, long idql, string mabn)
        {
            try
            {
                DataTable dataTable = new DataTable();
                string str = " select  " + columnIDQL + "," + columnMABN + "," + columnQUANHE + "," + columnHOTEN + "," + columnDIACHI + "," + columnDIENTHOAI + " from " + this._sSchema + "." + tableName + " where 1=1 ";
                if (idql != 0L)
                {
                    object obj2 = str;
                    str = string.Concat(new object[] { obj2, " and ", columnIDQL, "=", idql });
                }
                if (mabn != "")
                {
                    string str2 = str;
                    str = str2 + " and " + columnMABN + "='" + mabn + "'";
                }
                new NpgsqlDataAdapter(str + " order by " + columnIDQL + " desc", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "BN_THONGTINBN_LIENHE.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "BN_THONGTINBN_LIENHE.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnMABN, " = '", this._smabn, "',", columnQUANHE, " = :", columnQUANHE, ",", columnHOTEN, " = :", columnHOTEN, 
                    ",", columnDIACHI, " = :", columnDIACHI, ",", columnDIENTHOAI, " = '", this._sdienthoai, "'  where ", columnIDQL, " = ", this._deidql
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnQUANHE, NpgsqlDbType.Text, 200).Value = this._squanhe;
                command.Parameters.Add(columnHOTEN, NpgsqlDbType.Text, 200).Value = this._shoten;
                command.Parameters.Add(columnDIACHI, NpgsqlDbType.Text, 200).Value = this._sdiachi;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "BN_THONGTINBN_LIENHE.cs");
                return false;
            }
        }

        public static string columnDIACHI
        {
            get
            {
                return "diachi";
            }
        }

        public static string columnDIENTHOAI
        {
            get
            {
                return "dienthoai";
            }
        }

        public static string columnHOTEN
        {
            get
            {
                return "hoten";
            }
        }

        public static string columnIDQL
        {
            get
            {
                return "idql";
            }
        }

        public static string columnMABN
        {
            get
            {
                return "mabn";
            }
        }

        public static string columnQUANHE
        {
            get
            {
                return "quanhe";
            }
        }

        public string DIACHI
        {
            get
            {
                return this._sdiachi;
            }
            set
            {
                this._sdiachi = value;
            }
        }

        public string DIENTHOAI
        {
            get
            {
                return this._sdienthoai;
            }
            set
            {
                this._sdienthoai = value;
            }
        }

        public string HOTEN
        {
            get
            {
                return this._shoten;
            }
            set
            {
                this._shoten = value;
            }
        }

        public long IDQL
        {
            get
            {
                return this._deidql;
            }
            set
            {
                this._deidql = value;
            }
        }

        public string MABN
        {
            get
            {
                return this._smabn;
            }
            set
            {
                this._smabn = value;
            }
        }

        public string QUANHE
        {
            get
            {
                return this._squanhe;
            }
            set
            {
                this._squanhe = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "bn_thongtinbn_lienhe";
            }
        }
    }
}

